package cn.js.sangu.unitStore.controller;

import cn.hutool.core.util.StrUtil;
import cn.js.sangu.unitStore.common.LogType;
import cn.js.sangu.unitStore.common.MyLogsAOP;
import cn.js.sangu.unitStore.common.R;
import cn.js.sangu.unitStore.dto.RefreshDTO;
import cn.js.sangu.unitStore.entity.Logs;
import cn.js.sangu.unitStore.service.ChartsService;
import cn.js.sangu.unitStore.service.LogsService;
import cn.js.sangu.unitStore.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 说明：图表数据统计接口
 * 作者：linshuo
 * 日期：2024/8/7 8:40
 */
//@CrossOrigin //已有全局跨域设置CorsConfig
@RequestMapping("charts")
@RestController
public class ChartsController {
    
    static final String OPERATION_CHARTS = "数据统计";

    @Autowired
    LogsService logsService;

    @Autowired
    UserService userService;
    
    @Autowired
    ChartsService chartsService;

//    @GetMapping("refresh")
//    public R refresh(@RequestParam String lineText,@RequestParam String barText,@RequestParam String pieText){
//        return R.ok(chartsService.refresh(lineText,barText,pieText));
//    }

    @PostMapping("refresh")
    public R refresh(@RequestBody RefreshDTO dto){
        return R.ok(chartsService.refresh(dto));
    }

    @MyLogsAOP(operation = OPERATION_CHARTS,operaType = LogType.LOOKUP)
    @GetMapping("findByLast")
    public R findByLast(){
        return R.ok(chartsService.findByLast());
    }


    @MyLogsAOP(operation = OPERATION_CHARTS,operaType = LogType.LOOKUP)
    @GetMapping("findByPage")
    public R findByPage(@RequestParam Integer pageNum,
                        @RequestParam Integer pageSize,
                        @RequestParam String operation,
                        @RequestParam String operaType,
                        @RequestParam String username){
        QueryWrapper<Logs> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(operation),"operation",operation);
        wrapper.like(StrUtil.isNotBlank(operaType),"opera_type",operaType);
        wrapper.like(StrUtil.isNotBlank(username),"username",username);
        wrapper.orderByDesc("opera_time");

        //构建page对象
        Page<Logs> param = new Page<>(pageNum, pageSize);
        Page<Logs> res = logsService.page(param, wrapper);
        return R.ok(res);
    }

    @RequestMapping("save")
    public R save(@RequestBody Logs dto){
        logsService.save(dto);
        return R.ok("保存成功");
    }

    //安全起见，日志修改接口不对外开放
    @PutMapping("update")
    public R update(@RequestBody Logs dto){
//        logsService.updateById(dto);
        return R.ok("安全起见，日志修改接口不对外开放");
    }

    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable Integer id){
        logsService.removeById(id);
        return R.ok("删除成功");
    }

    @DeleteMapping("deleteBatch")
    public R deleteBatch(@RequestBody List<Integer> ids){
        logsService.removeBatchByIds(ids);
        return R.ok("批量删除成功");
    }

    @GetMapping("findById/{id}")
    public R findById(@PathVariable Integer id){
        return R.ok(logsService.getById(id));
    }

}
